package com.mmd.manage.service.impl;

import com.mmd.bean.CODE;
import com.mmd.common.entity.MmdRole;
import com.mmd.common.entity.MmdRoleMenu;
import com.mmd.exception.GameException;
import com.mmd.manage.dao.MmdRoleDao;
import com.mmd.manage.dao.MmdRoleMenuDao;
import com.mmd.manage.service.RoleService;
import com.mmd.manage.vo.RoleEo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MmdRoleDao mmdRoleDao;
    @Autowired
    private MmdRoleMenuDao mmdRoleMenuDao;

    @Override
    @Transactional
    public void addRole(RoleEo roleEo) {
        logger.info("添加角色参数；{}", roleEo.toString());
        if (StringUtils.isEmpty(roleEo.getRoleName())){
            throw new GameException(new CODE(9999, "请填写角色名称"));
        }
        MmdRole mmdRole = new MmdRole();
        mmdRole.setRoleName(roleEo.getRoleName());
        mmdRoleDao.insert(mmdRole);

        String[] menuIdArr = roleEo.getMenuIds().split(",");
        for (String menuId : menuIdArr){
            MmdRoleMenu mmdRoleMenu = new MmdRoleMenu();
            mmdRoleMenu.setRoleId(mmdRole.getRoleId());
            mmdRoleMenu.setMenuId(Long.parseLong(menuId));
            mmdRoleMenuDao.insert(mmdRoleMenu);
        }
    }

    @Override
    public List<MmdRole> getRoleList() {
        return mmdRoleDao.getRoleList();
    }
}
